//
//  AppDelegate.swift
//  新浪微博
//
//  Created by bin on 15/12/1.
//  Copyright © 2015年 XMG. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        // Override point for customization after application launch.
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.rootViewController = UIStoryboard(name: "Compose", bundle: nil).instantiateInitialViewController()
//        window?.rootViewController = defaultViewController()
        window?.makeKeyAndVisible()
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("setRootViewController:"), name: SwitchRootViewControllerNotification, object: nil)
        return true
    }
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
}

extension AppDelegate{
    
    private func isUpdate() -> Bool{
        let userDefaults = NSUserDefaults.standardUserDefaults()
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let sandBoxVersion = (userDefaults.objectForKey("Version") as? String) ?? "0"
        if currentVersion.compare(sandBoxVersion) == NSComparisonResult.OrderedDescending {
            userDefaults.setObject(currentVersion, forKey: "Version")
            userDefaults.synchronize()
            return true
        }
        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewControllerNotification, object: self, userInfo: ["vc" : "welcome"])
        return false
    }
    
    private func defaultViewController() -> UIViewController{
    
        guard TBUserAccount.isLogin() else{
            return UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()!
        }
        
        return isUpdate() ? UIStoryboard(name: "TBNewFeatureViewController", bundle: nil).instantiateInitialViewController()! : TBWelcomeViewController()
    
    }
    
    @objc private func setRootViewController(notification: NSNotification){
        switch notification.userInfo!["vc"] as! String{
        case "welcome" :
            window?.rootViewController = TBWelcomeViewController()
        case "main" :
            window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()!
        default :
            print("其他")
        }

    }

}

func TBLog<T>(message:T, fileName:String = __FILE__ ,methodName:String = __FUNCTION__,lineNumber:Int = __LINE__)->Void {
    #if DEBUG
        print("\((fileName as NSString).pathComponents.last!).\(methodName)[\(lineNumber)]:\(message)")
    #endif
}
